//
//  NYSubjectModel.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/3.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYSubjectModel.h"
@interface NYSubjectModel ()
/// 数组, 单选为一个选项，多选为多个选项
@property(nonatomic, strong) id exercises_answer;
@property(nonatomic, strong) NSArray * problem_answer;
@property(nonatomic, strong, nullable, readwrite) NSArray * exercises_answer_arr;

/// 正确答案（如果为选择题时，3表示c）可能为数组 单选为一个选项，多选为多个选项
@property(nonatomic, strong) id correct_answer;
@property(nonatomic, strong) NSArray * problem_correct_answer;
@property(nonatomic, strong, nullable, readwrite) NSArray * correct_answer_arr;

/// 错误答案
@property(nonatomic, strong) id wrong;
@property(nonatomic, strong, nullable, readwrite) NSArray <NSString *>* wrong_answer_arr;

/// 类型 id 等价于 genre/ 后台字段不明确
@property(nonatomic, assign) NYGenreType type_id;
// 统一后台字段
@property(nonatomic, copy) NSString *problem_title;
@property (nonatomic , copy) NSString * exercises_title;
@property(nonatomic, assign) NYQuestionType problem_type;
@property(nonatomic, strong) NSArray <NSString *>* exercises_answer_list;


@end
@implementation NYSubjectModel
+ (NSDictionary<NSString *,id> *)modelCustomPropertyMapper
{
        NYSubjectModel *model;
        return @{
                 @keypath(model,genre) : @[@keypath(model,genre),@keypath(model,type_id)],
                  @keypath(model,type_id) : @[@keypath(model,genre),@keypath(model,type_id)],
                  @keypath(model,question_type) : @[@keypath(model,question_type), @keypath(model,problem_type),@keypath(model,exercises_answer_type)],
                  @keypath(model,question_title) : @[@keypath(model,question_title), @keypath(model,problem_title), @keypath(model,exercises_title)],
                  @keypath(model,exercises_answer_list) : @[@keypath(model,exercises_answer_list), @keypath(model,problem_answer)],
                  @keypath(model,correct_answer) : @[@keypath(model,correct_answer), @keypath(model,problem_correct_answer)],
                 };
}
- (NYQuestionType)question_type
{
        if (_question_type <= 0 || _question_type >= NYQuestionTypeMAX) {
                if (!self.exercises_answer_list || (self.exercises_answer_list.count == 1 && NULLString(self.exercises_answer_list.firstObject))) {
                        _question_type = NYQuestionTypeFillBlanks;
                }
        }
        return _question_type;
}
- (NSArray<NSString *> *)exercises_answer_list
{
        if (_exercises_answer_list == nil) {
                return nil;
        }
        if ([_exercises_answer_list isKindOfClass:[NSArray class]] && _exercises_answer_list.count > 0 && !NULLString(_exercises_answer_list.firstObject)) {
                return _exercises_answer_list;
        } else if ([_exercises_answer_list isKindOfClass:[NSString class]]) {
                NSArray *arr = [(NSString *)self.exercises_answer_list jsonValueDecoded];
                if (arr != nil && [arr isKindOfClass:[NSArray class]]) {
                        NSMutableArray *array = [NSMutableArray arrayWithCapacity:arr.count];
                        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                if (!NULLObject(obj)) {
                                        [array addObject:obj];
                                }
                        }];
                        _exercises_answer_list = array;
                        return _exercises_answer_list;
                }
                else
                {
                        return @[(NSString *)_exercises_answer_list];
                }
        }
        return nil;
}
- (NSArray *)exercises_answer_arr
{
        if (_exercises_answer_arr.count > 0) {
                return _exercises_answer_arr;
        }
        else if (self.exercises_answer_list.count > 0 && !NULLString(self.exercises_answer_list.firstObject)) {
                _exercises_answer_arr = self.exercises_answer_list;
                return _exercises_answer_arr;
        }
        else if ([self.exercises_answer isKindOfClass:[NSArray class]]) {
                _exercises_answer_arr = self.exercises_answer;
                return _exercises_answer_arr;
        } else if ([self.exercises_answer isKindOfClass:[NSString class]]) {
                NSArray *arr = [(NSString *)self.exercises_answer jsonValueDecoded];
                if (arr != nil && [arr isKindOfClass:[NSArray class]]) {
                        NSMutableArray *array = [NSMutableArray arrayWithCapacity:arr.count];
                        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                if (!NULLObject(obj)) {
                                        [array addObject:obj];
                                }
                        }];
                        _exercises_answer_arr = array;
                        return _exercises_answer_arr;
                } else {
                        _exercises_answer_arr = @[(NSString *)self.exercises_answer];
                        return _exercises_answer_arr;
                }
        } else if (self.problem_answer && [self.problem_answer isKindOfClass:[NSArray class]]) {
                _exercises_answer_arr = self.problem_answer;
                return _exercises_answer_arr;
        }
        return _exercises_answer_arr;
}
- (NSArray *)correct_answer_arr
{
        if (_correct_answer_arr.count > 0) {
                return _correct_answer_arr;
        } else if ([self.correct_answer isKindOfClass:[NSArray class]]) {
                _correct_answer_arr = self.correct_answer;
                return _correct_answer_arr;
        } else if ([self.correct_answer isKindOfClass:[NSString class]]) {
                NSArray *arr = [(NSString *)self.correct_answer jsonValueDecoded];
                if (arr != nil && [arr isKindOfClass:[NSArray class]]) {
                        NSMutableArray *array = [NSMutableArray arrayWithCapacity:arr.count];
                        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                if (!NULLObject(obj)) {
                                        [array addObject:obj];
                                }
                        }];
                        _correct_answer_arr = array;
                        return _correct_answer_arr;
                } else {
                        _correct_answer_arr = @[(NSString *)self.correct_answer];
                        return _exercises_answer_arr;
                }
        } else if (self.problem_correct_answer && [self.problem_correct_answer isKindOfClass:[NSArray class]]) {
                _correct_answer_arr = self.problem_correct_answer;
                return _correct_answer_arr;
        }
        return _correct_answer_arr;
}
- (NSArray<NSString *> *)wrong_answer_arr
{
        if (_wrong_answer_arr.count > 0) {
                return _wrong_answer_arr;
        } else if ([self.wrong isKindOfClass:[NSArray class]]) {
                NSMutableArray *array = @[].mutableCopy;
                [(NSArray *)self.wrong enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        [array addObject:[NSString stringWithFormat:@"%@",obj]];
                }];
                _wrong_answer_arr = array;
                return _wrong_answer_arr;
        } else if ([self.wrong isKindOfClass:[NSString class]]) {
                NSArray *arr = [(NSString *)self.wrong jsonValueDecoded];
                if (arr!= nil && [arr isKindOfClass:[NSArray class]]) {
                        NSMutableArray *array = [NSMutableArray arrayWithCapacity:arr.count];
                        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                if (!NULLObject(obj)) {
                                        [array addObject:[NSString stringWithFormat:@"%@",obj]];
                                }
                        }];
                        _wrong_answer_arr = array;
                        return _wrong_answer_arr;
                } else {
                        if (self.question_type == NYQuestionTypeSingleChoice || self.question_type == NYQuestionTypeMultipleChoices)
                        {
                                NSString *wrongStr = (NSString *)self.wrong;
                                NSInteger len = [(NSString *)self.wrong length];
                                NSMutableArray *resArr = [NSMutableArray arrayWithCapacity:len];
                                for (int i = 0; i < len; i++)
                                {
                                        [resArr appendObject:[wrongStr substringWithRange:NSMakeRange(i, 1)]];
                                }
                                _wrong_answer_arr = resArr;
                        }
                        else
                        {
                                _wrong_answer_arr = @[(NSString *)self.wrong];
                        }
                       
                        return _wrong_answer_arr;
                }
        }
        return _wrong_answer_arr;
}
@end
